//
// Created by yunzeng on 2023/12/7.
//

#include "Message.h"

#include <iomanip>

Path::Path(Json::Value root) :
        endpointId(static_cast<EndpointId>(root["endpoint"].asInt())),
        clusterId(static_cast<ClusterId>(root["cluster"].asInt())),
        attributeId(static_cast<AttributeId>(root["attribute"].asInt())) {
}

Json::Value Path::Encode() const {
    Json::Value root;
    root["endpoint"] = endpointId;
    root["cluster"] = clusterId;
    root["attribute"] = attributeId;
    return root;
}

Path::Path(ClusterId cluster, AttributeId attribute) {
    clusterId = cluster;
    attributeId = attribute;
}

DataBlock::DataBlock(Json::Value root)
        : path(Path(root["path"])) {
    data = root["data"];
    if (root.isMember("data")) {
        status = root["status"].asInt();
    }
}

Json::Value DataBlock::Encode() const {
    Json::Value root;
    root["path"] = path.Encode();
    root["data"] = data;
    return root;
}

DataBlock::DataBlock(Path path, int value): path(path) {
    this->data = Json::Value(value);
}

DataBlock::DataBlock(Path path, bool value): path(path) {
    this->data = Json::Value(value);
}
